﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace Codeplus.Dhruvtaata.Web.Models.EntityAction {
    public abstract class CrudAction<T> : ICrudAction<T> where T : class {
        protected DatabaseContext dataContext = null;
        public void SetDataContext(DatabaseContext context) {
            this.dataContext = context;
        }
        public virtual void Create(T entity) {
            this.dataContext.Set<T>().Add(entity);
        }
        public IEnumerable<T> Retrieve(Func<T, bool> condition, Func<T, object> orderBy, int start, int range) {
            return this.dataContext.Set<T>()
                .OfType<T>()
                .Where(condition)
                .OrderBy(orderBy)
                .Skip(start)
                .Take(range);
        }
        public abstract void Update(T oldEntity, T newEntity);
        public virtual void Delete(T entity) {
            this.dataContext.Set<T>().Remove(entity);
        }
    }
}